#include "StdAfx.h"
#include "Simulator.h"
#include <string.h>

Simulator* Simulator::_this = NULL;
Simulator::Simulator(int w, int h, float scale, int maxfps){
	Width = w;
	Height = h;
	MaxFPS = maxfps;
	ScrScale = scale;
	ContainerWidth = (w*ScrScale)/4*2;
	x = (w*ScrScale - ContainerWidth)/2;
	y = -100;
#if defined(USE_GAMEPROJECT)
#if defined(USE_OPENFEINT)
	for(int i = 0; i< ACHIEVEMENT_NUM; i++){
		OfPoint[i] = 0;
	}
	FILE* _file_pointer = fopen(ACHIEVEMENT_BFILE, "rb");
	if (_file_pointer == NULL){
		return;	
	}
	fseek(_file_pointer, 0, SEEK_SET);
	for(int i = 0; i< ACHIEVEMENT_NUM; i++){
		int len = 0;
		fread(&len, 1, 4, _file_pointer);
		OfId[i] = new char[len+1];
		fread(OfId[i], 1, len, _file_pointer);
		OfId[i][len] = '\0';
	}
	for(int j = 0; j< ACHIEVEMENT_NUM; j++){
		OfPoint[j] = 0;
		fread(&OfPoint[j] , 1, 4, _file_pointer);
	}
	if(_file_pointer != NULL)
	fclose(_file_pointer);
#endif
#endif
	//init container
	hwndContainer=NULL;
	State = SIMULATOR_CONTAINER_STATE_SHOW;
	_current_text = NULL;
	
}
Simulator::~Simulator(){
	if(_current_text != NULL){
		delete[] _current_text;
		_current_text = NULL;
	}
}
Simulator* Simulator::getInstance(int w, int h, float scale, int maxfps){
	if(_this == NULL) _this = new Simulator(w,h,scale,maxfps);
	return _this;
}
void Simulator::initContainer(HWND parent,HINSTANCE h_instance,LPCWSTR classname){
	if(hwndContainer != NULL)return;
	hwndParent = parent;
	hInstance = h_instance;
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= Simulator::WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;//LoadIcon(hInstance, MAKEINTRESOURCE(IDI_IPHONEGAME));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;//MAKEINTRESOURCE(IDC_IPHONEGAME);
	wcex.lpszClassName	= L"SimulatorContainer";
	wcex.hIconSm		= NULL;//LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	ATOM a = RegisterClassEx(&wcex);
	DWORD error = GetLastError();
	//printf("register class: %d\n",error);
	hwndContainer = CreateWindowEx(0,
								L"SimulatorContainer",
								NULL,
								WS_CHILD|WS_VISIBLE|WS_BORDER,
								x,y,
								ContainerWidth,
								100,
								hwndParent,(HMENU)100,hInstance,NULL);//*/
	if(!hwndContainer){
		DWORD error = GetLastError();
		printf("some error here %d\n",error);
	}
	
}
LRESULT CALLBACK Simulator::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_CREATE:
		_this->hwndContainer = hWnd;
		//create edit		
		_this->hwndTxtInput = CreateWindow(TEXT("edit"),NULL,WS_CHILD|WS_VISIBLE|WS_BORDER|ES_LEFT|ES_AUTOHSCROLL,10,10,_this->ContainerWidth - 20,20,_this->hwndContainer,(HMENU)SIMULATOR_CONTAINER_TXTINPUT_HWND_ID,_this->hInstance,NULL);
		//create btn ok
		_this->hwndBtnOk = CreateWindow(TEXT("button"),TEXT("OK"),WS_CHILD|WS_VISIBLE|WS_BORDER,20,50,100,25,_this->hwndContainer,(HMENU)SIMULATOR_CONTAINER_BTNOK_HWND_ID,_this->hInstance,NULL);
		//create btn cancel
		_this->hwndBtnCancel = CreateWindow(TEXT("button"),TEXT("Cancel"),WS_CHILD|WS_VISIBLE|WS_BORDER,_this->ContainerWidth - 100 - 20,50,100,25,_this->hwndContainer,(HMENU)SIMULATOR_CONTAINER_BTNCANCEL_HWND_ID,_this->hInstance,NULL);
		//_this->openContainer();
		break;
	case WM_COMMAND:
		{
			DWORD cmd = LOWORD(wParam);
			DWORD action = HIWORD(wParam);
			switch(cmd){
			case SIMULATOR_CONTAINER_TXTINPUT_HWND_ID:
				break;
			case SIMULATOR_CONTAINER_BTNOK_HWND_ID:
				if(action == BN_CLICKED){
					_this->closeContainer();
					//get text
					int text_size = GetWindowTextLength(_this->hwndTxtInput);
					LPWSTR text = new wchar_t[text_size + 1];
					GetWindowText(_this->hwndTxtInput,text,text_size+1);
					text[text_size] = '\0';
					int size = WideCharToMultiByte(CP_ACP, 0, text, -1, 0, 0, NULL, NULL);
					int buf_size = SIZE_OF_INT8 + size -1;
					Bytes* bytes = new Bytes();
					bytes->_data_len = size - 1;
					bytes->_data = new char[size];
					WideCharToMultiByte(CP_ACP, 0, text, -1, bytes->_data, size, NULL, NULL);
					//bytes->_data[size] = '\0';
					//printf("U typesdf :%s %d\n",bytes->_data,bytes->_data_len);
					CByteBuffer* buf = BUFALLOCATE(buf_size);
					//printf("bufsize:%d\n",buf_size);
					PUTBBIN(buf,bytes);
					//throw event
					char* abc = buf->getBinary();
					//printf("sdf:%s\n",abc+1);
					delete abc;
					delete buf;
					delete bytes;
					delete[] text;
				}
				break;
			case SIMULATOR_CONTAINER_BTNCANCEL_HWND_ID:
				if(action == BN_CLICKED){
					_this->closeContainer();
				}
				break;
			}
		}
		break;
	/*case WM_PAINT:
		{
			if(SIMULATOR->State == SIMULATOR_CONTAINER_STATE_SHOW && y < 0){
				SIMULATOR->y += 2;
				SetWindowPos(SIMULATOR->hwndContainer,NULL,SIMULATOR->x,SIMULATOR->y,SIMULATOR->ContainerWidth,100,0);
			}
		}
		break;*/
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
void Simulator::correctTouch(int* x, int* y){
	*x = (*x)* (1.0f/ScrScale);
	*y = (*y)* (1.0f/ScrScale);
}
void Simulator::openContainer(){
	SIMULATOR->y = 0;
	//SetWindowPos(SIMULATOR->hwndContainer,NULL,SIMULATOR->x,SIMULATOR->y,SIMULATOR->ContainerWidth,100,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
	MoveWindow(SIMULATOR->hwndContainer,SIMULATOR->x,SIMULATOR->y,SIMULATOR->ContainerWidth,100,true);
	SetFocus(SIMULATOR->hwndTxtInput);
}

void Simulator::closeContainer(){
	SIMULATOR->y = -100;
	//SetWindowPos(SIMULATOR->hwndContainer,NULL,SIMULATOR->x,SIMULATOR->y,SIMULATOR->ContainerWidth,100,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
	MoveWindow(SIMULATOR->hwndContainer,SIMULATOR->x,SIMULATOR->y,SIMULATOR->ContainerWidth,100,true);
}
void Simulator::setInputText(const char* text, int len){
	if(_current_text != NULL)
	{
		delete[] _current_text;
		_current_text = NULL;
	}
	if(text != NULL){
		_current_text = new wchar_t[len + 1];
		swprintf(_current_text, len + 1, L"%hs", text);
	}
	SetWindowText(hwndTxtInput,_current_text);
}